<?php

namespace app\common\controller;

use app\admin\model\AdminAuthModel;
use app\admin\model\AdminRoleModel;
use app\admin\model\AdminUserModel;
use think\App;
use think\Controller;

class Base extends Controller
{
    /**
     * Base constructor.
     * @param App|null $app
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->auth();
    }

    /**
     * 首页显示对应权限
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function auth()
    {
        $session = getSession();//获取session
        $admin_id = $session['userid'];
        $admin_name = $session['name'];
        // 角色ID
        $admin_role_id = AdminUserModel::roleId($admin_id);

        if ($admin_name == 'laowang') {
            //超级管理员最大嘛
            $authA = AdminAuthModel::getLevelAll(['auth_level' => 0]);// 一级权限
            $authB = AdminAuthModel::getLevelAll(['auth_level' => 1]);// 二级权限
        } else {
            //权限 ids
            $role_auth_ids = AdminRoleModel::roleAuthIds($admin_role_id);
            $authA = AdminAuthModel::getLevelAll([['auth_level', '=', 0], ['auth_id', 'in', $role_auth_ids]]);// 一级权限
            $authB = AdminAuthModel::getLevelAll([['auth_level', '=', 1], ['auth_id', 'in', $role_auth_ids]]);// 二级权限
        }
        $auth = array('authA' => $authA, 'authB' => $authB);

        $this->assign('auth', $auth);
    }
}